package Thread;

public class Box {
    private int milkNum;
    //定一个成员变量 表示奶香的状态
    private boolean state = false;


    public synchronized void getMilkNum() {
        //如果没有奶 要等待生产奶
        if (!state) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("customer get mile:" + milkNum);
        //消费完毕
        state = false;
        notifyAll();

    }

    public Box() {
    }

    public Box(int milkNum) {
        this.milkNum = milkNum;
    }

    public synchronized void setMilkNum(int milkNum)  {
        // 如果有奶等待
        if (state) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 如果没有奶就生产奶

        System.out.println("producer has save milk:" + milkNum);
        this.milkNum = milkNum;

        state = true;
        notifyAll();
    }
}
